package com.qqs.socket;

import com.qqs.util.*;

public final class Command {
	private static final char CRC1 = 0x00;
	private static final char CRC2 = 0x00;
	public static final char SWITCH_ON = 0xFF;
	public static final char SWITCH_OFF = 0x00;

	public static char[] queryLight(char[] lightids) {
		char[] temp = { 0x55, 0x00, 0x0D, 0x06, 0x01, CRC1, 0xD1, 0x68, 0x80, 0x01, lightids[0], lightids[1],
				lightids[2], lightids[3], 0x88, 0x88, 0x88, 0x88, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, CRC2 };
		getFinallCmd(temp, lightids);
		return temp;
	}

	public static char[] ctrlLight(char[] chipids, char nodeChannel, char channelStatus) {
		char[] temp = { 0x55, 0x00, 0x0F, 0x06, 0x01, CRC1, 0xD1, 0x68, 0x80, 0x10, chipids[0], chipids[1], chipids[2],
				chipids[3], 0x00, 0x00, 0x88, 0x88, 0x88, 0x88, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, CRC2 };
		temp[14] = nodeChannel;
		temp[15] = channelStatus;
		getFinallCmd(temp, chipids);
		return temp;

	}

	public static char[] callId(int type) {
		char[] callid = { 0x55, 0x00, 0x09, 0x06, 0x01, CRC1, 0xD1, 0x68, 0x80, 0x00, 0x88, 0x88, 0x88, 0x88, 0x00,
				0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, CRC2 };
		if (type == 0) {
			// light
			callid[8] = 0x80;
		} else if (type == 1) {
			// led
			callid[8] = 0x82;
		} else if (type == 2) {
			// temp
			callid[8] = 0x84;
		} else if (type == 3) {
			// EEM
			callid[8] = 0x8F;
		} else if (type == 4) {
			// curtain
			callid[8] = 0x83;
		} else if (type == 5) {
			// music
			callid[8] = 0x8E;
			callid[9] = 0x40;
		} else if (type == 6) {
			// door
			callid[8] = 0x8E;
		}
		return getFinallCmd(callid);
	}

	public static char[] queryElectric(char[] id) {
		char EEM_Check[] = { 0x55, 0x00, 0x0E, 0x06, 0x01, CRC1, 0xD1, 0x68, 0x8F, 0x01, id[0], id[1], id[2], id[3],
				0xFF, 0x88, 0x88, 0x88, 0x88, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, CRC2 };
		char[] temp = EEM_Check;
		getFinallCmd(temp);
		return temp;
	}

	public static char[] queryLed(char[] chipids) {
		char temp1[] = { 0x55, 0x00, 0x0D, 0x06, 0x01, CRC1, 0xD1, 0x68, 0x82, 0x01, chipids[0], chipids[1], chipids[2],
				chipids[3], 0x88, 0x88, 0x88, 0x88, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, CRC2 };
		char[] temp = temp1;
		getFinallCmd(temp);
		return temp;

	}

	public static char[] ctrlCurtain(char[] chipids, char nodeChannel, char channelStatus) {
		char cmd[] = { 0x55, 0x00, 0x0F, 0x06, 0x01, 0x3E, 0xD1, 0x68, 0x83, 0x10, chipids[0], chipids[1], chipids[2],
				chipids[3], nodeChannel, channelStatus, 0x88, 0x88, 0x88, 0x88, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF,
				0xFF, CRC2 };
		char[] temp = cmd;
		getFinallCmd(temp);
		return temp;
	}

	public static char[] ctrlLed(char[] chipids, int R, int G, int B) {
		char cR = (char) (R * 100 / 255);
		char cG = (char) (G * 100 / 255);
		char cB = (char) (B * 100 / 255);
		char cmd[] = { 0x55, 0x00, 0x11, 0x06, 0x01, CRC1, 0xD1, 0x68, 0x82, 0x10, chipids[0], chipids[1], chipids[2],
				chipids[3], 0x07, cB, cG, cR, 0x88, 0x88, 0x88, 0x88, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, CRC2 };
		char[] temp = cmd;
		getFinallCmd(temp);
		return temp;
	}

	public static char[] ctrlDoor(char[] id, char ctrl) {
		char cmd[] = { 0x55, 0x00, 0x0E, 0x06, 0x01, CRC1, 0xD1, 0x68, 0x8E, ctrl, id[0], id[1], id[2], id[3], 0xFF,
				0x88, 0x88, 0x88, 0x88, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xDF };
		char[] temp = cmd;
		getFinallCmd(temp);
		return temp;
	}

	public static char[] ctrlMusic(char[] musicid, String ctrlstr) {
		char Music_query[] = { 0x55, 0x00, 0x0E, 0x06, 0x01, CRC1, 0xD1, 0x68, 0x8E, 0x41, musicid[0], musicid[1],
				musicid[2], musicid[3], 0x00, 0x88, 0x88, 0x88, 0x88, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xDF };
		char Music_open[] = { 0x55, 0x00, 0x0F, 0x06, 0x01, CRC1, 0xD1, 0x68, 0x8E, 0x42, musicid[0], musicid[1],
				musicid[2], musicid[3], 0xFF, 0xFF, 0x88, 0x88, 0x88, 0x88, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
				0xDF };
		char Music_close[] = { 0x55, 0x00, 0x0F, 0x06, 0x01, CRC1, 0xD1, 0x68, 0x8E, 0x42, musicid[0], musicid[1],
				musicid[2], musicid[3], 0xFF, 0x00, 0x88, 0x88, 0x88, 0x88, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
				0xDF };
		char Music_play[] = { 0x55, 0x00, 0x0F, 0x06, 0x01, CRC1, 0xD1, 0x68, 0x8E, 0x43, musicid[0], musicid[1],
				musicid[2], musicid[3], 0xFF, 0xFF, 0x88, 0x88, 0x88, 0x88, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
				0xDF };
		char Music_pause[] = { 0x55, 0x00, 0x0F, 0x06, 0x01, CRC1, 0xD1, 0x68, 0x8E, 0x43, musicid[0], musicid[1],
				musicid[2], musicid[3], 0xFF, 0x88, 0x88, 0x88, 0x88, 0x88, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
				0xDF };
		char Music_up[] = { 0x55, 0x00, 0x0F, 0x06, 0x01, CRC1, 0xD1, 0x68, 0x8E, 0x44, musicid[0], musicid[1],
				musicid[2], musicid[3], 0xFF, 0xFF, 0x88, 0x88, 0x88, 0x88, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
				0xDF };
		char Music_down[] = { 0x55, 0x00, 0x0F, 0x06, 0x01, CRC1, 0xD1, 0x68, 0x8E, 0x44, musicid[0], musicid[1],
				musicid[2], musicid[3], 0xFF, 0x00, 0x88, 0x88, 0x88, 0x88, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
				0xDF };
		char Music_add[] = { 0x55, 0x00, 0x0F, 0x06, 0x01, CRC1, 0xD1, 0x68, 0x8E, 0x46, musicid[0], musicid[1],
				musicid[2], musicid[3], 0xFF, 0xFF, 0x88, 0x88, 0x88, 0x88, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
				0xDF };
		char Music_cut[] = { 0x55, 0x00, 0x0F, 0x06, 0x01, CRC1, 0xD1, 0x68, 0x8E, 0x46, musicid[0], musicid[1],
				musicid[2], musicid[3], 0xFF, 0x00, 0x88, 0x88, 0x88, 0x88, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
				0xDF };
		char Music_soundOn[] = { 0x55, 0x00, 0x0F, 0x06, 0x01, CRC1, 0xD1, 0x68, 0x8E, 0x47, musicid[0], musicid[1],
				musicid[2], musicid[3], 0xFF, 0xFF, 0x88, 0x88, 0x88, 0x88, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
				0xDF };
		char Music_soundOff[] = { 0x55, 0x00, 0x0F, 0x06, 0x01, CRC1, 0xD1, 0x68, 0x8E, 0x47, musicid[0], musicid[1],
				musicid[2], musicid[3], 0xFF, 0x00, 0x88, 0x88, 0x88, 0x88, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
				0xDF };
		char[] temp = Music_query;
		if ("query".equals(ctrlstr))
			temp = Music_query;
		else if ("open".equals(ctrlstr))
			temp = Music_open;
		else if ("close".equals(ctrlstr))
			temp = Music_close;
		else if ("play".equals(ctrlstr))
			temp = Music_play;
		else if ("pause".equals(ctrlstr))
			temp = Music_pause;
		else if ("up".equals(ctrlstr))
			temp = Music_up;
		else if ("down".equals(ctrlstr))
			temp = Music_down;
		else if ("soundUp".equals(ctrlstr))
			temp = Music_add;
		else if ("soundDown".equals(ctrlstr))
			temp = Music_cut;
		else if ("soundOn".equals(ctrlstr))
			temp = Music_soundOn;
		else if ("soundOff".equals(ctrlstr))
			temp = Music_soundOff;
		getFinallCmd(temp, musicid);
		return temp;

	}

	public static char[] queryTemp(char[] tmcids) {
		char contrlTemperature[] = { 0x55, 0x00, 0x0D, 0x06, 0x01, CRC1, 0xD1, 0x68, 0x84, 0x01, tmcids[0], tmcids[1],
				tmcids[2], tmcids[3], 0x88, 0x88, 0x88, 0x88, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, CRC2 };
		char[] temp = contrlTemperature;
		getFinallCmd(temp, tmcids);
		return temp;
	}

	public static char[] ctrlTemp(char[] chipids, char swich, char temperatureH, char temperatureL, char wind,
			char mode) {
		char contrlTemperature[] = { 0x55, 0x00, 0x12, 0x06, 0x01, CRC1, 0xD1, 0x68, 0x84, 0x10, chipids[0], chipids[1],
				chipids[2], chipids[3], swich, temperatureH, temperatureL, wind, mode, 0x88, 0x88, 0x88, 0x88, 0x00,
				0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, CRC2 };
		char[] temp = contrlTemperature;
		getFinallCmd(temp, chipids);
		return temp;
	}

	public static char[] learnOn(char[] chipids, char[] onchipids, char operate, char channel, char directionChar,
			char mode) {
		char learn_on[] = { 0x55, 0x00, 0x14, 0x06, 0x01, 0x00, 0xD1, 0x68, 0x80, operate, chipids[0], chipids[1],
				chipids[2], chipids[3], directionChar, onchipids[0], onchipids[1], onchipids[2], onchipids[3], mode,
				channel, 0x88, 0x88, 0x88, 0x88, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00 };
		char[] temp = learn_on;
		getFinallCmd(temp, chipids);
		return temp;
	}

	public static char[] learnBody(char[] chipids, char[] bodychipids, char operate, char channel) {
		char learn_body[] = { 0x55, 0x00, 0x14, 0x06, 0x01, 0x00, 0xD1, 0x68, 0x80, operate, chipids[0], chipids[1],
				chipids[2], chipids[3], 0x20, 0x16, bodychipids[0], bodychipids[1], bodychipids[2], bodychipids[3],
				channel, 0x88, 0x88, 0x88, 0x88, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00 };
		char[] temp = learn_body;
		getFinallCmd(temp, chipids);
		return temp;
	}

	public static char[] setIllumination(char[] chipids, char p1) {
		char update_parameter[] = { 0x55, 0x00, 0x0F, 0x06, 0x01, 0x00, 0xD1, 0x68, 0x80, 0x21, chipids[0], chipids[1],
				chipids[2], chipids[3], p1, 0xFF, 0x88, 0x88, 0x88, 0x88, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
				0x00 };
		char[] temp = update_parameter;
		getFinallCmd(temp, chipids);
		return temp;
	}

	public static char[] setDelayTime(char[] chipids, char p1, char p2) {
		char update_parameter[] = { 0x55, 0x00, 0x0F, 0x06, 0x01, 0x00, 0xD1, 0x68, 0x80, 0x22, chipids[0], chipids[1],
				chipids[2], chipids[3], p1, p2, 0x88, 0x88, 0x88, 0x88, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
				0x00 };
		char[] temp = update_parameter;
		getFinallCmd(temp, chipids);
		return temp;
	}

	public static char[] setPriorityLevel(char[] chipids, char p1) {
		char update_parameter[] = { 0x55, 0x00, 0x0F, 0x06, 0x01, 0x00, 0xD1, 0x68, 0x80, 0x23, chipids[0], chipids[1],
				chipids[2], chipids[3], p1, 0xFF, 0x88, 0x88, 0x88, 0x88, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
				0x00 };
		char[] temp = update_parameter;
		getFinallCmd(temp, chipids);
		return temp;
	}

	public static char[] setPriorityTime(char[] chipids, char p1, char p2) {
		char update_parameter[] = { 0x55, 0x00, 0x0F, 0x06, 0x01, 0x00, 0xD1, 0x68, 0x80, 0x24, chipids[0], chipids[1],
				chipids[2], chipids[3], p1, p2, 0x88, 0x88, 0x88, 0x88, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
				0x00 };
		char[] temp = update_parameter;
		getFinallCmd(temp, chipids);
		return temp;
	}

	/**
	 * 设置ID，计算CRC，形成可发送到硬件的命令
	 * 
	 * @param cmds
	 * @param ids
	 */
	private static char[] getFinallCmd(char[] cmds, char[] ids) {
		cmds[10] = ids[0];
		cmds[11] = ids[1];
		cmds[12] = ids[2];
		cmds[13] = ids[3];
		// crc1
		int ntemp = 4;
		char[] vdata1 = new char[ntemp];
		for (int i = 0; i < ntemp; i++) {
			vdata1[i] = cmds[1 + i];
		}
		cmds[5] = CRC.Set_CRC(vdata1);

		// crc2
		ntemp = cmds.length - 7;

		char[] vdata2 = new char[ntemp];
		for (int i = 0; i < ntemp; i++) {
			vdata2[i] = cmds[6 + i];
		}
		cmds[cmds.length - 1] = CRC.Set_CRC(vdata2);
		return cmds;
	}

	// callID
	private static char[] getFinallCmd(char[] ContrlData) {
		// crc1
		int ntemp = 4;
		char[] vdata1 = new char[ntemp];
		for (int i = 0; i < ntemp; i++) {
			vdata1[i] = ContrlData[1 + i];
		}
		ContrlData[5] = CRC.Set_CRC(vdata1);

		// crc2
		ntemp = ContrlData.length - 7;

		char[] vdata2 = new char[ntemp];
		for (int i = 0; i < ntemp; i++) {
			vdata2[i] = ContrlData[6 + i];
		}
		ContrlData[ContrlData.length - 1] = CRC.Set_CRC(vdata2);
		return ContrlData;

	};

}
